<?php
/**
 *@file bloglistcontroller.php
 *@author Zhe.Zhou(zhouzhe@haibao.com)
 *@date 2015-9-10 下午2:50:11
 *@brief  
 */
namespace haibao\cms\business\control\pc\channel\blog;

use haibao\cms\data\control\BlogController as BlogControllerData;

class BlogListController extends BlogController{
	public function __construct($updateTime = null,$controlId,$isPublish){
		parent::__construct($updateTime,$controlId,$isPublish);
	}
	
	/**
	 * 获取模版内容
	 * @param : String $content
	 */
	public function displayTemplateHtml(){
		$controlData = $this->getParameter(self::$CONTROL_DATA);
		$content = $this->getParameter(self::$CONTROL_CONTENT);
		
		$currentPage = $this->getParameter(self::$CURRENTPAGE);
		$path = $this->generateHtmlPath(__CLASS__);
		
		$dataType = $controlData['manual']?$controlData['manual']['type']:1;
		$this->assign('dataType',$dataType);
		if($dataType == 1){
			$blogList = $blogHotList = array();
			list($cateInfo,$cateId) = self::getCateInfo($this->getParameter(self::$SITE_CHANNEL_TAG),true);
			if($cateId){
				$blogList = $this->getNewList($cateId,$currentPage,BlogControllerData::ORDER_BY_DATE);
				$blogHotList = $this->getNewList($cateId, $currentPage, BlogControllerData::ORDER_BY_VIRE);
			}
			$blogIdArr = $blogHotIdArr = array();
			foreach ($blogList as $key=>$list){
				$blogList[$key]['CreateTime'] = $this->formatDate($list['CreateTime']);
				$blogIdArr[] = $list['BlogId'];
			}
			foreach ($blogHotList as $key=>$list){
				$blogHotList[$key]['CreateTime'] = $this->formatDate($list['CreateTime']);
				$blogHotIdArr[] = $list['BlogId'];
			}
			if($blogIdArr){
				$blogPics = $this->getBlogIntroPic($blogIdArr);
			}
			if($blogHotIdArr){
				$blogHotPics = $this->getBlogIntroPic($blogHotIdArr);
			}
			foreach ($blogList as $key=>$list){
				if(array_key_exists ($list['BlogId'],$blogPics)){
					$blogList[$key]['Filename'] = $blogPics[$list['BlogId']];
				}else{
					$blogList[$key]['Filename'] = self::DEFAULT_SHOW_IMAGE_URL;
				}
				$blogList[$key]['Url'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BOZHU)."/blog/".$list['BlogId'].".htm";
				$blogList[$key]['UserUrl'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BOZHU)."/user/".$list['Uid']."/";
			}
			foreach ($blogHotList as $key=>$list){
				if(array_key_exists ($list['BlogId'],$blogHotPics)){
					$blogHotList[$key]['Filename'] = $blogHotPics[$list['BlogId']];
				}else{
					$blogHotList[$key]['Filename'] = self::DEFAULT_SHOW_IMAGE_URL;
				}
				$blogHotList[$key]['Url'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BOZHU)."/blog/".$list['BlogId'].".htm";
				$blogHotList[$key]['UserUrl'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BOZHU)."/user/".$list['Uid']."/";
			}
			$this->assign('blogList',$blogList);
			$this->assign('blogHotList',$blogHotList);
			$this->assign('channelName',$cateInfo['name']);
			$this->assign('attentionType',\haibao\www\model\data\Favorite::ATTENTION_TYPE_BLOGGER);
			$this->assign('iMarkType',\haibao\www\model\data\Favorite::ID_TYPE_BLOG_NUM);
			
			/**
			 * pager类
			*/
			$pager = new \haibao\frame\data\query\Pager(\haibao\cms\data\control\BlogController::PAGE_NUM,$currentPage);
			$pager->setResultCount($this->getResultCountByObjectId($cateId,\haibao\cms\data\control\BlogController::COUNT_TYPE_CATE_ID));
			$this->assign('pager',$pager);
			$this->assign('pageUrl',\haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BOZHU)."/cate/".$cateId."/0/");
			
			$this->assign('siteTemplateId',$this->getParameter(self::$SITE_TEMPLATE_ID));
			
			
		}else{
			//获取博主的博客列表
			$userId = $controlData['manual']['queryId'];
			$blogList = $this->getBloggerList($userId, $currentPage);
			$blogIds = array_keys($blogList);
			$blogPics = $this->getBlogIntroPic($blogIds);
			foreach ($blogList as $key=>$list){
				$blogList[$key]['Url'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BOZHU)."/blog/".$list['blogid'].".htm";
				$blogList[$key]['UserUrl'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BOZHU)."/user/".$list['uid']."/";
				$blogList[$key]['BlogId'] = $list['blogid'];
				$blogList[$key]['Uid'] = $list['uid'];
				$blogList[$key]['UserName'] = $list['username'];
				$blogList[$key]['Title'] = $list['subject'];
				$blogList[$key]['Filename'] = isset($blogPics[$list['blogid']]) ? $blogPics[$list['blogid']] : self::DEFAULT_SHOW_IMAGE_URL;
				$blogList[$key]['CreateTime'] =  $this->formatDate(date("Y-m-d H:i:s",$list['dateline']));
			}
 			$userInfo = $this->getBloggerInfoById($userId);
 			$this->assign('channelName',$userInfo['uname']." Blog List");
			
			/**
			 * pager类
			 */
			$pager = new \haibao\frame\data\query\Pager(\haibao\cms\data\control\BlogController::PAGE_NUM,$currentPage);
			$resultCount = $this->getResultCountByObjectId($userId,\haibao\cms\data\control\BlogController::COUNT_TYPE_UID);
			$pager->setResultCount($resultCount);
			$this->assign('blogList',$blogList);
			if($resultCount > \haibao\cms\data\control\BlogController::PAGE_NUM){
				$this->assign('pager',$pager);
			}
			$this->assign('pageUrl',\haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BOZHU)."/user/".$userId."/");
			$this->assign('attentionType',\haibao\www\model\data\Favorite::ATTENTION_TYPE_BLOGGER);
			$this->assign('iMarkType',\haibao\www\model\data\Favorite::ID_TYPE_BLOG_NUM);
			$this->assign('siteTemplateId',$this->getParameter(self::$SITE_TEMPLATE_ID));
		}
		$this->generateHtml($path,$content);
		$content = $this->getHtml($path);
		if($dataType == 2){
			$content = preg_replace('/\bpreg:bloggerId\b/isU',$userId,$content);
		}
		$content = $this->replaceCDNLink($content);
		return $content;
		
	}
	
	/**
	 * 按照所选的orderBy去取出数据
	 */
	private function getNewList($cateId,$page,$orderBy){
		return $this->getBlogListByCateId($cateId,$page,$orderBy);
	}
	
	private function getBloggerList($userId,$page){
		return $this->getBlogListByUserIds($userId,10,\haibao\cms\data\control\BlogController::ORDER_BY_DATE,$page);
	}
	
}